package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

import com.keyroy.game.puzzle_rpg.models.attribute.AttributeUtil;
import com.keyroy.game.puzzle_rpg.models.attribute.Param;
import com.keyroy.game.puzzle_rpg.models.attribute.RandomParam;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;

public class ParamEditor extends Shell {
	private Spinner random_chance;
	private Combo param_type;
	private Spinner param_value;
	private Combo random_type;
	private Spinner random_min;
	private Spinner random_max;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ParamEditor shell = new ParamEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */

	public ParamEditor(Display display, Param param, boolean containRandomValue, final EventListener<Param> listener) {
		super(display, SWT.DIALOG_TRIM);
		init(containRandomValue, listener);
		param_type.select(AttributeUtil.getParamIndex(param.getClass()));
		param_type.setEnabled(false);
		if (param instanceof RandomParam) {
			enableRandom();
			RandomParam randomValue = (RandomParam) param;
			random_type.select(AttributeUtil.getParamIndex(randomValue.clazz));
			random_type.setEnabled(false);
			random_min.setSelection(randomValue.getMin());
			random_max.setSelection(randomValue.getMax());
			random_chance.setSelection(randomValue.chance);
		} else {
			param_value.setSelection(param.value);
		}

	}

	public ParamEditor(Display display, Param param, final EventListener<Param> listener) {
		this(display, param, true, listener);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public ParamEditor(Display display, final EventListener<Param> listener) {
		this(display, true, listener);
	}

	public ParamEditor(Display display, boolean containRandomValue, final EventListener<Param> listener) {
		super(display, SWT.DIALOG_TRIM);
		init(containRandomValue, listener);
	}

	private void init(boolean containRandomValue, final EventListener<Param> listener) {
		setMinimumSize(new Point(450, 300));
		setLayout(new FormLayout());

		Label lblNewLabel = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.right = new FormAttachment(0, 71);
		fd_lblNewLabel.top = new FormAttachment(0, 10);
		fd_lblNewLabel.left = new FormAttachment(0, 10);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("\u5C5E\u6027\u7C7B\u578B : ");

		param_type = new Combo(this, SWT.READ_ONLY);
		param_type.setItems(AttributeUtil.getParamTypes(containRandomValue));

		FormData fd_attribute_type = new FormData();
		fd_attribute_type.right = new FormAttachment(0, 434);
		fd_attribute_type.top = new FormAttachment(0, 7);
		fd_attribute_type.left = new FormAttachment(0, 77);
		param_type.setLayoutData(fd_attribute_type);

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(lblNewLabel, 14);
		fd_lblNewLabel_1.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("\u5C5E\u6027\u6570\u503C : ");

		Button btnNewButton = new Button(this, SWT.NONE);

		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.bottom = new FormAttachment(100, -10);
		fd_btnNewButton.right = new FormAttachment(param_type, 0, SWT.RIGHT);
		fd_btnNewButton.left = new FormAttachment(0, 326);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u5B8C\u6210");
		param_type.select(0);

		Group group = new Group(this, SWT.NONE);
		group.setText("\u968F\u673A\u6570\u5C5E\u6027");
		group.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_group = new FormData();
		fd_group.bottom = new FormAttachment(0, 219);
		fd_group.left = new FormAttachment(0, 10);
		fd_group.right = new FormAttachment(100, -10);
		group.setLayoutData(fd_group);

		Composite composite = new Composite(group, SWT.NONE);

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(10, 13, 59, 17);
		lblNewLabel_2.setText("\u5C5E\u6027\u7C7B\u578B : ");

		random_type = new Combo(composite, SWT.READ_ONLY);
		random_type.setItems(AttributeUtil.getParamTypes());
		random_type.remove(random_type.getItemCount() - 1);
		random_type.setEnabled(false);
		random_type.setBounds(75, 10, 333, 25);

		random_min = new Spinner(composite, SWT.BORDER);
		random_min.setMinimum(1);
		random_min.setEnabled(false);
		random_min.setBounds(75, 41, 333, 23);

		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setBounds(10, 44, 61, 17);
		lblNewLabel_3.setText("\u6700\u5C0F\u503C : ");

		random_max = new Spinner(composite, SWT.BORDER);
		random_max.setEnabled(false);
		random_max.setMaximum(65535);
		random_max.setMinimum(-65535);
		random_max.setSelection(5);
		random_max.setBounds(75, 70, 333, 23);

		Label lblNewLabel_4 = new Label(composite, SWT.NONE);
		lblNewLabel_4.setBounds(10, 73, 61, 17);
		lblNewLabel_4.setText("\u6700\u5927\u503C : ");

		Label lblNewLabel_5 = new Label(composite, SWT.NONE);
		lblNewLabel_5.setBounds(10, 102, 48, 17);
		lblNewLabel_5.setText("\u51FA\u73B0\u6982\u7387 : ");

		random_chance = new Spinner(composite, SWT.BORDER);
		random_chance.setEnabled(false);
		random_chance.setBounds(75, 99, 333, 23);
		random_chance.setMinimum(-100);
		random_chance.setSelection(100);

		param_value = new Spinner(this, SWT.BORDER);
		fd_group.top = new FormAttachment(0, 67);
		param_value.setMaximum(65535);
		param_value.setMinimum(-65535);
		FormData fd_attribute_value = new FormData();
		fd_attribute_value.top = new FormAttachment(param_type, 6);
		fd_attribute_value.left = new FormAttachment(lblNewLabel_1, 8);
		fd_attribute_value.right = new FormAttachment(100, -10);
		param_value.setLayoutData(fd_attribute_value);
		createContents();

		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					Class<? extends Param> clazz = AttributeUtil.getParamClass(param_type.getSelectionIndex());
					try {
						Param param = clazz.newInstance();
						if (param instanceof RandomParam) {
							RandomParam randomValue = (RandomParam) param;
							clazz = AttributeUtil.getParamClass(random_type.getSelectionIndex());
							randomValue.set(clazz, random_min.getSelection(), random_max.getSelection());
							randomValue.chance = random_chance.getSelection();
						} else {
							param.value = param_value.getSelection();
						}

						listener.onEvent(param);
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}

				dispose();
			}
		});

		param_type.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Class<? extends Param> clazz = AttributeUtil.getParamClass(param_type.getSelectionIndex());
				if (clazz != null && clazz.equals(RandomParam.class)) {
					enableRandom();
				} else {
					disableRandom();
				}
			}
		});
	}

	private final void disableRandom() {
		random_type.setEnabled(false);
		random_min.setEnabled(false);
		random_max.setEnabled(false);
		random_chance.setEnabled(false);
		param_value.setEnabled(true);
	}

	private final void enableRandom() {
		random_type.setEnabled(true);
		random_min.setEnabled(true);
		random_max.setEnabled(true);
		random_chance.setEnabled(true);
		param_value.setEnabled(false);

	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u5C5E\u6027\u7F16\u8F91");
		setSize(450, 283);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
